/**
 * RBAC权限管理相关类型定义
 */

// 基础响应类型
export interface ApiResponse<T = any> {
  success: boolean
  message: string
  data: T
  total?: number
  page?: number
  size?: number
}

// 用户状态枚举
export enum UserStatus {
  ACTIVE = 'active',
  INACTIVE = 'inactive',
  LOCKED = 'locked',
  PENDING = 'pending'
}

// 权限操作枚举
export enum PermissionAction {
  READ = 'read',
  CREATE = 'create',
  UPDATE = 'update',
  DELETE = 'delete',
  MANAGE = 'manage'
}

// 资源类型枚举
export enum ResourceType {
  MODULE = 'module',
  PAGE = 'page',
  BUTTON = 'button',
  API = 'api',
  DATA = 'data'
}

// 用户接口
export interface User {
  user_id?: number  // 后端登录时返回user_id
  id?: number       // 保持兼容性
  username: string
  email: string
  real_name: string
  employee_id?: string
  phone?: string
  avatar_url?: string
  department?: string
  position?: string
  status?: UserStatus
  is_superuser?: boolean
  last_login_at?: string
  created_at?: string
  updated_at?: string
  roles?: Role[]
  permissions?: string[]  // 后端登录时返回的权限列表
}

// 角色接口
export interface Role {
  id: number
  name: string
  code: string
  description?: string
  level: number
  parent_id?: number
  is_active: boolean
  is_system: boolean
  created_at: string
  updated_at?: string
  created_by?: number
  updated_by?: number
  user_count?: number
  permissions?: Permission[]
  parent_role?: Role
  child_roles?: Role[]
}

// 资源接口
export interface Resource {
  id: number
  name: string
  code: string
  type: ResourceType
  module: string
  path?: string
  description?: string
  level: number
  parent_id?: number
  icon?: string
  sort_order?: number
  is_active: boolean
  is_system: boolean
  created_at: string
  updated_at?: string
  permissions?: Permission[]
  parent_resource?: Resource
  child_resources?: Resource[]
}

// 权限接口
export interface Permission {
  id: number
  name: string
  code: string
  description?: string
  action: PermissionAction
  resource_id: number
  conditions?: string
  is_active: boolean
  is_system: boolean
  created_at: string
  updated_at?: string
  resource: Resource
}

// 用户角色关联接口
export interface UserRole {
  id: number
  user_id: number
  role_id: number
  assigned_by: number
  assigned_at: string
  expires_at?: string
  is_active: boolean
  user?: User
  role?: Role
}

// 角色权限关联接口
export interface RolePermission {
  id: number
  role_id: number
  permission_id: number
  assigned_by: number
  assigned_at: string
  is_active: boolean
  role?: Role
  permission?: Permission
}

// API请求类型

// 角色相关请求
export interface RoleCreateRequest {
  name: string
  code: string
  description?: string
  parent_id?: number
  is_active?: boolean
}

export interface RoleUpdateRequest {
  name?: string
  description?: string
  is_active?: boolean
}

export interface RoleListQuery {
  page?: number
  size?: number
  search?: string
  is_active?: boolean
  parent_id?: number
}

// 用户相关请求
export interface UserCreateRequest {
  username: string
  email: string
  real_name: string
  password: string
  employee_id?: string
  phone?: string
  department?: string
  position?: string
  status?: UserStatus
  role_ids?: number[]
}

export interface UserUpdateRequest {
  email?: string
  real_name?: string
  employee_id?: string
  phone?: string
  department?: string
  position?: string
  status?: UserStatus
}

export interface UserListQuery {
  page?: number
  size?: number
  search?: string
  status?: UserStatus
  department?: string
  role_id?: number
}

// 权限相关请求
export interface PermissionListQuery {
  resource_id?: number
  action?: PermissionAction
  module?: string
}

export interface PermissionAssignRequest {
  role_id: number
  permission_ids: number[]
}

export interface UserRoleAssignRequest {
  user_id: number
  role_ids: number[]
}

export interface PermissionCheckRequest {
  resource: string
  action: string
  context?: Record<string, any>
}

// 资源相关请求
export interface ResourceListQuery {
  type?: ResourceType
  module?: string
  parent_id?: number
}

// API响应类型
export interface RoleListResponse extends ApiResponse<Role[]> {}
export interface RoleDetailResponse extends ApiResponse<Role> {}
export interface UserListResponse extends ApiResponse<User[]> {}
export interface UserDetailResponse extends ApiResponse<User> {}
export interface PermissionListResponse extends ApiResponse<Permission[]> {}
export interface ResourceListResponse extends ApiResponse<Resource[]> {}

// 权限检查结果
export interface PermissionCheckResult {
  has_permission: boolean
  resource: string
  action: string
  context?: Record<string, any>
}

// 用户权限信息
export interface UserPermissionInfo {
  user_id: number
  permissions: string[]
  roles: Array<{
    id: number
    name: string
    code: string
  }>
  accessible_resources: string[]
}

// 登录相关
export interface LoginRequest {
  username: string
  password: string
  remember_me?: boolean
  captcha_code?: string
  session_id?: string
}

export interface LoginResponse {
  access_token: string
  refresh_token: string
  token_type: string
  expires_in: number
  user_info: User
}

// JWT Token负载
export interface JWTPayload {
  user_id: number
  username: string
  email: string
  is_superuser: boolean
  exp: number
  iat: number
}

// 权限上下文
export interface PermissionContext {
  user_id: number
  roles: string[]
  permissions: string[]
  resources: string[]
  department?: string
  is_superuser: boolean
}

// 表单验证错误
export interface ValidationError {
  field: string
  message: string
}

// 分页信息
export interface PaginationInfo {
  page: number
  size: number
  total: number
  pages: number
  has_next: boolean
  has_prev: boolean
}

// 排序信息
export interface SortInfo {
  field: string
  order: 'asc' | 'desc'
}

// 列表查询基础接口
export interface BaseListQuery {
  page?: number
  size?: number
  search?: string
  sort_field?: string
  sort_order?: 'asc' | 'desc'
}

// 审计日志
export interface AuditLog {
  id: number
  user_id: number
  action: string
  resource_type: string
  resource_id?: number
  details?: Record<string, any>
  ip_address?: string
  user_agent?: string
  created_at: string
  user?: User
}

export default {
  UserStatus,
  PermissionAction,
  ResourceType
}